package com.platform.common.utils;

import com.platform.common.config.Global;
import com.platform.upms.model.UpmsUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class UserUtils {

    public static Integer getUserId() {
        Integer id = -1;
        try {
            UpmsUser user = getUser();
            if(user != null) {
                id = user.getId();
            }
        } catch (Exception e) { }
        return id;
    }

    public static Integer getUserOrgId() {
        Integer orgId = -1;
        try {
            UpmsUser user = getUser();
            if(user != null) {
                orgId = user.getOrgId();
            }
        } catch (Exception e) { }
        return orgId;
    }

    public static UpmsUser getUser() {
        UpmsUser user;
        try {
            user = (UpmsUser) getSession().getAttribute(Global.UPMSUSER);
        } catch (Exception e) {
            user = null;
        }
        return user;
    }

    public static HttpSession getSession() {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getSession();
    }

    public static boolean isHighestPower() {
        boolean flag;
        Integer orgId = getUserOrgId();
      if(orgId == 0) {
            flag = true;
        } else {
            flag = false;
        }
        return flag;
    }

}
